package com.yisu.auction.api.core.service.impl;

import com.google.common.collect.Lists;
import com.yisu.auction.api.core.mapper.AddressMapper;
import com.yisu.auction.api.core.mapper.GoodsTypeMapper;
import com.yisu.auction.api.core.mapper.PrizeArchiveMapper;
import com.yisu.auction.api.core.mapper.PrizeMapper;
import com.yisu.auction.api.core.service.BaseService;
import com.yisu.auction.api.core.service.IPrizeService;
import com.yisu.auction.api.domain.request.prizePackage.*;
import com.yisu.auction.api.domain.vo.PrizePackageVos.HistoricalTendencyVo;
import com.yisu.auction.api.domain.vo.PrizePackageVos.PrizeInformationVo;
import com.yisu.auction.api.domain.vo.goodsInfoPackageVos.*;
import com.yisu.auction.api.domain.vo.indexPackageVos.PrizeListVo;
import com.yisu.auction.ewf.entity.Address;
import com.yisu.auction.ewf.entity.Prize;
import org.apache.calcite.linq4j.Linq4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * author: 赵杰
 * Business Description：竞品业务
 * createTime：2017/8/16
 */
@Service("IPrizeService")
public class PrizeServiceImpl extends BaseService implements IPrizeService {
    @Resource
    private PrizeMapper prizeMapper;
    @Resource
    private PrizeArchiveMapper prizeArchiveMapper;
    @Resource
    private GoodsTypeMapper goodsTypeMapper;
    @Resource
    private AddressMapper addressMapper;

    /**
     * author: 赵杰
     * Business Description：通过列表类型，获取奖品列表
     *
     * @param prizeListRequest
     * @return
     */
    @Override
    public List<PrizeListVo> getPrizeList(PrizeListRequest prizeListRequest) {
        List<PrizeListVo> prizeListVos = Lists.newArrayList();
        try {
            switch (prizeListRequest.getListType()) {
                case 1: //正在竞拍的商品
                    prizeListVos = prizeMapper.getPrizeListForUnderway(prizeListRequest);
                    break;
                case 2:// 新手专区
                    prizeListVos = prizeMapper.getPrizeListForNoviceArea(prizeListRequest);
                    break;
                case 3:// 个人在拍专区
                    prizeListVos = prizeMapper.getPrizeListForMySelf(prizeListRequest);
                    break;
            }
        } catch (Exception e) {
            logger.error("通过列表类型，获取奖品列表异常：" + e.getMessage());
            return prizeListVos;
        }
        return prizeListVos;
    }

    /**
     * author: 赵杰
     * Business Description：通过竞品ID获取竞品详情
     *
     * @param prizeDetailRequest 含竞品ID
     * @return
     */
    @Override
    public PrizeVo getPrizeDetailByPrizeId(PrizeDetailRequest prizeDetailRequest) {
        PrizeVo prizeVo = prizeMapper.getPrizeDetailByPrizeId(prizeDetailRequest);
        if (prizeVo == null) {
            prizeVo = prizeArchiveMapper.getPrizeDetailByPrizeId(prizeDetailRequest);
        }
        return prizeVo;
    }

    /**
     * author: 赵杰
     * Business Description：通过prizeId获取竞品实体
     *
     * @param prizeId
     * @return
     */
    @Override
    public Prize getPrizeModelByPrizeId(Long prizeId) {
        return getPrizeById(prizeId);
    }

    /**
     * author: 赵杰
     * Business Description：通过竞品ID获取下期prizeId
     *
     * @param prizeId
     * @return
     */
    @Override
    public long getNextPeriodPrizeIdByPrizeId(long prizeId) {
        Prize prize=getPrizeById(prizeId);
        return prizeMapper.getNextPeriodPrizeIdByGoodsId(prize.getGoodsId());
    }

    /**
     * author: 赵杰
     * Business Description：通过商品ID获取下期prizeId
     *
     * @param goodsId
     * @return
     */
    @Override
    public long getNextPeriodPrizeIdByGoodsId(long goodsId) {
        return prizeMapper.getNextPeriodPrizeIdByGoodsId(goodsId);
    }

    /**
     * author: 赵杰
     * Business Description：更新竞品
     *
     * @param updateModel
     * @return
     */
    @Override
    public boolean updatePrizeSelective(Prize updateModel) {
        Prize prize = prizeMapper.selectByPrimaryKey(updateModel.getId());
        if (prize == null) {
            return prizeArchiveMapper.updateByPrimaryKeySelective(updateModel) > 0;
        } else {
            return prizeMapper.updateByPrimaryKeySelective(updateModel) > 0;
        }
    }

    /**
     * author: 赵杰
     * Business Description：通过竞品ID往期记录
     *
     * @param previousRecordRequest 含prizeId
     * @return
     */
    @Override
    public List<PreviousRecordVo> getPreviousRecordListByPrizeId(PreviousRecordRequest previousRecordRequest) {
        return prizeMapper.getPreviousRecordListByPrizeId(previousRecordRequest);
    }

    /**
     * author: 赵杰
     * Business Description：获取最新揭晓列表
     *
     * @param latestAnnouncedRequest
     * @return
     */
    @Override
    public List<LatestAnnouncedVo> getLatestAnnouncedList(LatestAnnouncedRequest latestAnnouncedRequest) {
        return prizeMapper.getLatestAnnouncedList(latestAnnouncedRequest);
    }


    /**
     * author: 赵杰
     * Business Description：获取商品类型列表
     *
     * @return
     */
    @Override
    public List<GoodsTypeVo> getGoodsTypeList(GoodsTypeRequest goodsTypeRequest) {
        return goodsTypeMapper.getGoodsTypeList(goodsTypeRequest);
    }

    /**
     * author: 赵杰
     * Business Description：通过类型ID获取竞品ID
     *
     * @param prizeListRequest 类型ID
     * @return
     */
    @Override
    public List<PrizeListVo> getPrizeListByGoodsTypeId(PrizeListRequest prizeListRequest) {
        return prizeMapper.getPrizeListByGoodsTypeId(prizeListRequest);
    }


    /**
     * author:黄萧林
     * Business Description:通过状态获取我的竞拍数据
     *
     * @param mineAuctionListRequest
     * @return
     */
    @Override
    public List<PrizeVo> getMineAuctionList(MineAuctionListRequest mineAuctionListRequest) {
        List<PrizeVo> prizeVos = Lists.newArrayList();
        if (mineAuctionListRequest.getStatus() == 0) {
            prizeVos = prizeMapper.getAllMineAuctionList(mineAuctionListRequest);
            Integer totalPage = mineAuctionListRequest.getPagesize();
            Integer surplusPageSize = mineAuctionListRequest.getPagesize();
            if (!CollectionUtils.isEmpty(prizeVos)) {
                surplusPageSize = totalPage - prizeVos.size();
            }
            if (surplusPageSize > 0) {
                if (surplusPageSize != mineAuctionListRequest.getPagesize()) {
                    mineAuctionListRequest.setPageindex(1);
                }
                List<PrizeVo> replenishList = prizeArchiveMapper.getAllMineAuctionList(mineAuctionListRequest);
                if (!CollectionUtils.isEmpty(replenishList)) {
                    for (PrizeVo item : replenishList) {
                        prizeVos.add(item);
                    }
                }
            }
        } else if (mineAuctionListRequest.getStatus() == 1) {
            return prizeMapper.getProcessMineAuctionList(mineAuctionListRequest);
        } else if (mineAuctionListRequest.getStatus() == 2) {
            prizeVos = prizeMapper.getWinningMineAuctionList(mineAuctionListRequest);
            Integer totalPage = mineAuctionListRequest.getPagesize();
            Integer surplusPageSize = mineAuctionListRequest.getPagesize();
            if (!CollectionUtils.isEmpty(prizeVos)) {
                surplusPageSize = totalPage - prizeVos.size();
            }
            if (surplusPageSize > 0) {
                if (surplusPageSize != mineAuctionListRequest.getPagesize()) {
                    mineAuctionListRequest.setPageindex(1);
                }
                List<PrizeVo> replenishList = prizeArchiveMapper.getWinningMineAuctionList(mineAuctionListRequest);
                if (!CollectionUtils.isEmpty(replenishList)) {
                    for (PrizeVo item : replenishList) {
                        prizeVos.add(item);
                    }
                }
            }
        } else if (mineAuctionListRequest.getStatus() == 3) {
            prizeVos = prizeMapper.getLosingMineAuctionList(mineAuctionListRequest);
            Integer totalPage = mineAuctionListRequest.getPagesize();
            Integer surplusPageSize = mineAuctionListRequest.getPagesize();
            if (!CollectionUtils.isEmpty(prizeVos)) {
                surplusPageSize = totalPage - prizeVos.size();
            }
            if (surplusPageSize > 0) {
                if (surplusPageSize != mineAuctionListRequest.getPagesize()) {
                    mineAuctionListRequest.setPageindex(1);
                }
                List<PrizeVo> replenishList = prizeArchiveMapper.getLosingMineAuctionList(mineAuctionListRequest);
                if (!CollectionUtils.isEmpty(replenishList)) {
                    for (PrizeVo item : replenishList) {
                        prizeVos.add(item);
                    }
                }
            }
        } else if (mineAuctionListRequest.getStatus() == 4) {
            prizeVos = prizeMapper.getWaitSignForMineAuctionList(mineAuctionListRequest);
            Integer totalPage = mineAuctionListRequest.getPagesize();
            Integer surplusPageSize = mineAuctionListRequest.getPagesize();
            if (!CollectionUtils.isEmpty(prizeVos)) {
                surplusPageSize = totalPage - prizeVos.size();
            }
            if (surplusPageSize > 0) {
                if (surplusPageSize != mineAuctionListRequest.getPagesize()) {
                    mineAuctionListRequest.setPageindex(1);
                }
                List<PrizeVo> replenishList = prizeArchiveMapper.getWaitSignForMineAuctionList(mineAuctionListRequest);
                if (!CollectionUtils.isEmpty(replenishList)) {
                    for (PrizeVo item : replenishList) {
                        prizeVos.add(item);
                    }
                }
            }
        } else if (mineAuctionListRequest.getStatus() == 5) {
            prizeVos = prizeMapper.getWaitShowOrderListMineAuctionList(mineAuctionListRequest);
            Integer totalPage = mineAuctionListRequest.getPagesize();
            Integer surplusPageSize = mineAuctionListRequest.getPagesize();
            if (!CollectionUtils.isEmpty(prizeVos)) {
                surplusPageSize = totalPage - prizeVos.size();
            }
            if (surplusPageSize > 0) {
                if (surplusPageSize != mineAuctionListRequest.getPagesize()) {
                    mineAuctionListRequest.setPageindex(1);
                }
                List<PrizeVo> replenishList = prizeArchiveMapper.getWaitShowOrderListMineAuctionList(mineAuctionListRequest);
                if (!CollectionUtils.isEmpty(replenishList)) {
                    for (PrizeVo item : replenishList) {
                        prizeVos.add(item);
                    }
                }
            }
        }
        return prizeVos;
    }

    /**
     * author:黄萧林
     * Business Description：获取历史走势页面商品信息
     *
     * @param historicalTendencyRequest
     * @return
     */
    @Override
    public PrizeInformationVo getPrizeInfo(HistoricalTendencyRequest historicalTendencyRequest) {
        Prize prize = getPrizeById(historicalTendencyRequest.getPrizeId());
        if (!StringUtils.isEmpty(prize.getImage())) {
            prize.setImage(Linq4j.asEnumerable(prize.getImage().split(",")).toList().get(0));
        }
        PrizeInformationVo PrizeInformationVo = new PrizeInformationVo();
        PrizeInformationVo.setTransactionPrice(prize.getTransactionPrice());
        PrizeInformationVo.setImage(prize.getImage());
        PrizeInformationVo.setName(prize.getName());
        return PrizeInformationVo;
    }

    /**
     * author:黄萧林
     * Business Description:获取历史走势数据列表
     *
     * @param historicalTendencyRequest
     * @return
     */
    @Override
    public List<HistoricalTendencyVo> getHistoricalTendencyListByPrizeId(HistoricalTendencyRequest historicalTendencyRequest) {
        Prize prize = getPrizeById(historicalTendencyRequest.getPrizeId());
        return prizeMapper.getHistoricalTendencyListByPrizeId(prize.getGoodsId());
    }

    /**
     * author:赵杰
     * Business Description:通过prizeId获取竞拍结果
     *
     * @param prizeId
     * @return
     */
    @Override
    public AuctionResultVo getAuctionResultByPrizeId(long prizeId) {
        return prizeMapper.getAuctionResultByPrizeId(prizeId);
    }

    /**
     * 绑定收货地址
     *
     * @param prizeId   prizeId
     * @param addressId 收货地址绑定ID
     * @return
     */
    @Override
    public boolean bandingAddress(long prizeId, long addressId) {
        Prize prize = prizeMapper.selectByPrimaryKey(prizeId);
        Address address = addressMapper.selectByPrimaryKey(addressId);
        if (address == null) {
            return false;
        }
        if (prize == null) {
            Prize updatePrize = new Prize();
            updatePrize.setAddress(address.getProvince() + address.getCity() + (StringUtils.isEmpty(address.getRegion()) ? "" : address.getRegion()) +
                    (StringUtils.isEmpty(address.getCountry()) ? "" : address.getCountry()) + address.getDetail());
            updatePrize.setRecer(address.getLinker());
            updatePrize.setId(prizeId);
            updatePrize.setLinkPhone(address.getMobile());
            return prizeArchiveMapper.updateByPrimaryKeySelective(updatePrize) > 0;
        } else {
            Prize updatePrize = new Prize();
            updatePrize.setId(prizeId);
            updatePrize.setAddress(address.getProvince() + address.getCity() + (StringUtils.isEmpty(address.getRegion()) ? "" : address.getRegion()) +
                    (StringUtils.isEmpty(address.getCountry()) ? "" : address.getCountry()) + address.getDetail());
            updatePrize.setRecer(address.getLinker());
            updatePrize.setLinkPhone(address.getMobile());
            return prizeMapper.updateByPrimaryKeySelective(updatePrize) > 0;
        }
    }

}
